package com.hfut.eeg.controller;

import cn.hutool.core.lang.Console;
import com.hfut.eeg.common.api.CommonResult;
import com.hfut.eeg.common.component.EEGcache;
import com.hfut.eeg.common.component.EEGfileListener;
import com.hfut.eeg.common.component.Receivecache;
import com.hfut.eeg.dto.CheckState;
import com.hfut.eeg.dto.ReportInfo;
import com.hfut.eeg.dto.TodayErrWaves;
import com.hfut.eeg.service.PatientInfoService;
import com.hfut.eeg.service.ReportService;
import com.hfut.eeg.service.SysInfoService;
import com.hfut.eeg.socket.AiServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * 启动Ai服务的Controller
 */
@Controller
public class WaveController {

    @Autowired
    private SysInfoService sysInfoService;
    @Autowired
    private AiServer aiServer;
    @Autowired
    private CheckState checkState;
    @Autowired
    private EEGcache eeGcache;
    @Autowired
    private PatientInfoService patientInfoService;
    @Autowired
    private TodayErrWaves todayErrWaves;
    @Autowired
    private EEGfileListener listener;
    @Autowired
    private ReportService reportService;
    @Autowired
    private Receivecache receivecache;

    /**
     * 开始检查
     * @param patientId
     * @return
     */
    @RequestMapping("/startCheck")
    @ResponseBody
    public CommonResult startCheck(String patientId){
        //检验pantientId有效性
        if(patientId==null || patientInfoService.getNameById(patientId)==null){
            return CommonResult.failed("请传输正确的病人ID");
        }
        eeGcache.setPatientId(patientId);
        aiServer.setEegId(patientId);
        listener.setCheckState(true);
        return CommonResult.success("成功");
    }

    /**
     * 结束检查
     * @param doctorId
     * @return
     */
    @RequestMapping("/stopCheck")
    @ResponseBody
    public CommonResult stopCheck(String doctorId){
        //停止EEG数据收集
        listener.setCheckState(false);
        String patientId = eeGcache.getPatientId();
        //进行修改病人的就诊状态，更新为1
        patientInfoService.changePatientStatus(patientId);
        //更新报告的异常波信息
        patientInfoService.updateAbWavesNum(patientId,aiServer.getErrNum());
        patientInfoService.updatePatientCostTime(patientId);
        //更新当日的系统异常波
        todayErrWaves.setErrWaves(todayErrWaves.getErrWaves()+aiServer.getErrNum());
        //更新系统总共的异常波信息
        sysInfoService.updateSysAbWaveNum((int)sysInfoService.getSysInfo().get("abWaveNum")+aiServer.getErrNum());

        //生成报告表数据
        ReportInfo reportInfo = new ReportInfo();
        reportInfo.setReportId(System.currentTimeMillis());
        reportInfo.setTime(new Date());
        reportInfo.setPatientId(eeGcache.getPatientId());
        reportInfo.setEegId(patientInfoService.getPatientLatestEEGId(eeGcache.getPatientId()));
        Console.log(reportInfo);
        reportService.insertReport(reportInfo);

        //重置信息
        aiServer.reset();
        listener.reset();
        receivecache.reset();
        eeGcache.reset();

        return CommonResult.success("成功");
    }
}
